package edu.gatech.cs4235.group3.domain;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import edu.gatech.cs4235.group3.exception.OneTimePadException;

/**
 * A Message object is the messages that are used within the system. The Message object is a 80 columns
 * by 50 rows character message.
 *
 * @author Jeffrey Starker
 *
 */
public class Message {
	
	/** The text of the message. */
	private char[][] text;
	
	public Message() {
		text = new char[50][80];
	}
	
	/**
	 * Creates a message given a file name.
	 *
	 * @param fileName The name of the file to create a Message from.
	 */
	public Message(final String fileName) throws OneTimePadException {
		
		File file = new File(fileName);
		
		FileInputStream fileInputStream = null;
		BufferedInputStream bufferedInputStream = null;
		DataInputStream data = null;
		
		text = new char[50][80];
		
		try {
			fileInputStream = new FileInputStream(file);
			
			bufferedInputStream = new BufferedInputStream(fileInputStream);
			
			data = new DataInputStream(bufferedInputStream);
			
			// Read each character of the file and add it to the text array.
			for (int i = 0; i < text.length; i ++) {
				
				for (int j = 0; j < text[0].length; j ++) {
					char readChar = (char) data.readByte();
					text[i][j] = readChar;
				}
			}
			
			fileInputStream.close();
			bufferedInputStream.close();
			data.close();
			
		} catch (final EOFException eofException) {
			
			OneTimePadException exception = new OneTimePadException("invalidFileFormat", fileName);
			throw exception;
			
		} catch (final IOException ioException ) {
			System.out.println(ioException);
			OneTimePadException exception = new OneTimePadException("unableToReadFile", fileName);
			throw exception;
		} 
	}
	
	/**
	 * Saves a message to the file system at the specified file name.
	 * 
	 * @param fileName The file name of the file to write.
	 * @throws OneTimePadException If we cannot write the file.
	 */
	public void saveMessage(final String fileName) throws OneTimePadException {
		
		try {
			FileWriter writer = new FileWriter(fileName);
			BufferedWriter bufferedWriter = new BufferedWriter(writer);
			
			StringBuilder builder = new StringBuilder();
			
			
			for (int i = 0; i < text.length; i++) {
				for (int j = 0; j < text[0].length; j++) {
					builder.append(text[i][j]);
				}
			}
			
			bufferedWriter.write(builder.toString());
			bufferedWriter.close();
		} catch (final IOException ioe) {
			
			OneTimePadException padException = new OneTimePadException("unableToWriteFile", fileName);
			throw padException;
		}
		
		
		
		
	}
	
	/**
	 * Returns the text of the message.
	 *
	 * @return The text of the message.
	 */
	public char[][] getText() {
		return text;
	}
	
	/**
	 * Sets the text of the message.
	 *
	 */
	public void setText(char[][] newText) {
		text = newText;
	}
	
	/**
	 * Prints out the Message in Standard System out.
	 */
	public void printMessage() {
		System.out.println(this.toString());
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		
		for (int i = 0; i < text.length; i++) {
			for (int j = 0; j < text[0].length; j++) {
				builder.append(text[i][j]);
			}
			
			builder.append("\n");
		}
		
		return builder.toString();
	}
}
